/*
 * motor_task.h
 *
 *  Created on: Oct 12, 2024
 *      Author: Jinming
 */

#ifndef INC_MOTOR_TASK_H_
#define INC_MOTOR_TASK_H_

#include "main.h"
#include "motor_drv.h"
#include "mi_motor_drv.h"
//#include "timer_2ms.h"

#define MI_MOTOR_NUM		2
#define BO_MOTOR_NUM		4

typedef enum
{
	MOTOR_1 = 0,
	MOTOR_2,
	MOTOR_3,
	MOTOR_4,

	MOTOR_SUM,
}MOTOR_TABLE_e;

enum
{
	MOTOR_BUS_GROUP_0 = 0,
	MOTOR_BUS_GROUP_1,

	MOTOR_BUS_GROUP_MAX,
};

enum
{
	MOTOR_MSG_GET_INFO = 0xA5,
	MOTOR_MSG_UPDATE_DEV,
	MOTOR_MSG_SEND_COMMAND,
	MOTOR_MSG_HOST_LOST,
};

typedef enum
{
	MOTOR_TYPE_BOMINGWEI = 0,		//博铭维电机
	MOTOR_TYPE_XIAOMI,				//小米电机
}MotorType_t;

typedef struct
{
	int32_t p;			//位置, 度*10000
	int32_t v;			//速度, RPM*10000
	int32_t t;			//力矩
	int16_t temperature;	//温度,℃
	uint16_t errorcode;		//错误代码
}MotorInfo_t;			//电机当前状态信息

typedef struct
{
	MotorType_t type;				//电机型号
	uint8_t busGroup;			//总线归属组
	uint32_t canId;
	BOOL isExist;			//该电机是否存在
	MotorUnit_t motor;		//电机驱动单元
	MotorInfo_t info;		//电机信息

	uint16_t lostCnt;		//通信丢失计数
}Motor_t;

typedef struct
{
	MotorType_t type;				//电机型号
	uint8_t busGroup;			//总线归属组
	uint32_t canId;
	BOOL isExist;			//该电机是否存在
	MiMotorUnit_t motor;	//小米电机
	MotorInfo_t info;		//电机信息

	uint16_t lostCnt;		//通信丢失计数
}MiMotor_t;

typedef struct
{
	MotorType_t type;				//电机型号
	uint8_t busGroup;				//总线归属组
	uint8_t index;					//激活索引，对应Spine板四个CAN口，用于操纵电机
	void *pMotorHandle;				//电机句柄
}ActiveMotor_t;						//激活的

int motor_task_init(void);
void motor_task_loop(void);
void motor_info_query_interrupt(void);
void host_lost_to_motor(void);
void motor_control_mode(uint8_t motorId, uint8_t mode);

void motor_dev_update(void);
void host_command_to_motor(void);

//测试指令
void motor_stop(uint8_t motorId);
void motor_task_torque_set(uint8_t motorId, float torque);
void motor_task_speed_set(uint8_t motorId, float speed);
void motor_task_position_set(uint8_t motorId, float pos);
void motor_task_runctrl_set(uint8_t motorId, float pos, float speed, float kp, float ki);

#endif /* INC_MOTOR_TASK_H_ */
